package com.promis.widget.lettersort;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.promis.util.CollectionUtils;
import com.promis.widget.R;

/**
 * Created by jidanbo on 2018/1/12.
 */

public class HotspotAdapter extends RecyclerView.Adapter<HotspotAdapter.ViewHolder> {

    private LayoutInflater mInflater;
    private Context context;
    private String[] hotspots;

    public HotspotAdapter(Context context, String[] hotspots) {
        this.context = context;
        this.mInflater = LayoutInflater.from(context);
        this.hotspots = hotspots;
    }

    @Override
    public HotspotAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.adapter_letter_hotspot_item, parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final HotspotAdapter.ViewHolder holder, final int position) {

        if (CollectionUtils.isNullOrEmpty(hotspots)) return;

        holder.textView.setText(hotspots[position]);

        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onItemClick(holder.itemView, position);
                }
            });

        }
    }

    @Override
    public int getItemCount() {
        if (CollectionUtils.isNullOrEmpty(hotspots)) return 0;
        else return hotspots.length;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        private TextView textView;

        public ViewHolder(View itemView) {
            super(itemView);

            textView = (TextView) itemView.findViewById(R.id.adapter_letter_sort_hotspot_txtv_content);
        }
    }

    //**********************itemClick************************
    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }
}
